<div class="layui-fluid">
  <div class="layui-row">
    <form action="" method="post" class="layui-form layui-form-pane">
      <div class="layui-form-item">
        <input type="hidden" name="id" value="{$id}">
      </div>
      <div class="layui-form-item">
        <label for="username" class="layui-form-label">
          <span class="x-red">*</span>用户名
        </label>
        <div class="layui-input-inline">
          <input type="text" id="username" name="username" value="{$data.username}" required="" lay-verify="required"
            autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label for="admin_role_id" class="layui-form-label">
          <span class="x-red">*</span>所属角色
        </label>
        <div class="layui-input-inline">
          <select name="admin_role_id">
            {foreach $roles as $v}
            <option value="{$v.id}" {if $v.id===$data.admin_role_id}selected{/if}>{$v.name}</option>
            {/foreach}
          </select>
        </div>
      </div>
      <div class="layui-form-item">
        <label for="password" class="layui-form-label">
          {if !$id}<span class="x-red">*</span>{/if}密码
        </label>
        <div class="layui-input-inline">
          <input type="text" id="password" name="password" autocomplete="off" class="layui-input" {if $id}
            placeholder="保存不变" lay-verify="newpass" {else} lay-verify="required|pass" required="" {/if}>
        </div>
      </div>
      <div class="layui-form-item">
        <label for="expiration_date" class="layui-form-label">
          <span class="x-red">*</span>到期时间
        </label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" id="expiration_date" name="expiration_date"
            value="{$data.expiration_date}" required="" lay-verify="required" autocomplete="off">
        </div>
      </div>
      <div class="layui-form-item">
        <button class="layui-btn" lay-submit="" lay-filter="save">保存</button>
      </div>
    </form>
  </div>
</div>
<script>
  layui.use(['form', 'layer', 'laydate'], function () {
    $ = layui.jquery;
    var form = layui.form
      , laydate = layui.laydate
      , layer = layui.layer;

    form.verify({
      newpass: function (value, item) {
        if (value.length < 6 && value.length != 0) {
          return '密码最少为6位';
        }
      }
    });

    //执行一个laydate实例
    laydate.render({
      elem: '#expiration_date' //指定元素
    });

    //监听提交
    form.on('submit(save)', function (data) {
      //发异步，把数据提交给php
      main.ajaxPost(
        {
          url: "{:url('save')}",
          data: data.field,
        },
        function (data) {
          setTimeout(function () {
            // 获得frame索引
            var index = parent.layer.getFrameIndex(window.name);
            //表格重载
            parent.layui.table.reload('dataGrid');
            //关闭当前frame 
            parent.layer.close(index);
          }, 1000);
        }
      )
      return false;
    });
  });
</script>